import com.mochimedia.ui.*;

/**
* ...
* @author $(DefaultUser)
*/

class com.mochimedia.ui.ScrollBar extends Cell {

    private var _type:String = "scrollbar";
    
    private var _orientation:Number;
    
    private var _btn_back:BButton;
    private var _btn_fwd:BButton;
    
    private var _slider:Slider;

    private var _pageRatio:Number = 1;
    
    private var _targetCell:Cell;
    public function get targetCell():Cell { return _targetCell; }
    
    public function set targetCell(value:Cell):Void {
        
        _targetCell = value;
        
        if (_targetCell != null) {
            
            _targetCell.maskContent = true;
            
            clear();
            create();
            
            onTargetCellChange();
            
            _targetCell.addEventListener(EVENT_CHANGE, this, onTargetCellChange);
            _targetCell.addEventListener(EVENT_FOCUS, this, onTargetCellShow);
            _targetCell.addEventListener(EVENT_BLUR, this, onTargetCellHide);
                
        }
        
    }
    

    //
    //
    public function ScrollBar (container:MovieClip, width:Number, height:Number, orientation:Number, position:Position, style:Style) {
        
        init(container, width, height, orientation, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, width:Number, height:Number, orientation:Number, position:Position, style:Style):Void {
        
        super.init(container, _width, _height, false, false, 0, position, style);
        
        _orientation = orientation;
        
        _width = width;
        _height = height;

    }
    
    //
    //
    public function create ():Void {
        
        super.create();
        
        if (_targetCell == undefined) return;
        
        var offset:Number = 0;
        
        if (_parentCell != null) {
            
            if (_parentCell.style.border) offset = _parentCell.style.borderWidth;
            
            if (_orientation == Position.ORIENTATION_HORIZONTAL) {
                
                _width = _targetCell.width;
                _height = 20;
            
                if (_parentCell.style.border && _position.margin_top + _position.margin_bottom < _parentCell.style.borderWidth && _width > _parentCell.width - _position.margin_right - _position.margin_left - _parentCell.style.borderWidth * 2) _width -= offset * 2;
                
                _position = new Position( { margins: [_position.margin_top, _position.margin_right, _position.margin_bottom, _position.margin_left], placement: Position.PLACEMENT_ABSOLUTE, zindex: 1000, top: _targetCell.y + _targetCell.height, left: _targetCell.x } );
                
            } else {

                _width = 20;
                _height = _targetCell.height;
                
               if (_parentCell.style.border && _position.margin_top + _position.margin_bottom < _parentCell.style.borderWidth && _height > _parentCell.height - _position.margin_top - _position.margin_bottom - _parentCell.style.borderWidth * 2) _height -= offset * 2;
                
                _position = new Position( { margins: [_position.margin_top, _position.margin_right, _position.margin_bottom, _position.margin_left], placement: Position.PLACEMENT_ABSOLUTE, zindex: 1000, top: _targetCell.y , left: _targetCell.x + _targetCell.width } );
                
            }
            
        }
        
        var _scrollbarStyle:Style = _style.clone();
        _scrollbarStyle.borderWidth = 2;
        _scrollbarStyle.borderColor = ColorTools.getTintedColor(_style.buttonColor, _style.backgroundColor, 0.5);

        var unselectedColor1:Number = ColorTools.getTintedColor(_style.backgroundColor, _style.borderColor, 0.3);
        var unselectedColor2:Number = ColorTools.getTintedColor(_style.backgroundColor, _style.borderColor, 0.1);
        
        var pos:Position;
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) {
            
            pos = new Position( { placement: Position.PLACEMENT_FLOAT } );

            _btn_back = BButton(addChild(new BButton(null, Create.ICON_ARROW_LEFT, null, 20, 20, false, false, false, pos, _scrollbarStyle)));
            _slider = Slider(addChild(new Slider(null, _width - 40, _height, _orientation, 0, pos, _style)));
            _btn_fwd = BButton(addChild(new BButton(null, Create.ICON_ARROW_RIGHT, null, 20, 20, false, false, false, pos, _scrollbarStyle)));
            
        } else {
            
            pos = new Position( { margin_bottom: 2 } );
            
            DrawingMethods.roundedRect(_mc, true, 0, 0, 20, 20, "0", [unselectedColor2, unselectedColor1], [50, 50], [0, 255]);
            DrawingMethods.roundedRect(_mc, false, 0, 22, _width, _height - 44, "0", [unselectedColor2, unselectedColor1], [50, 50], [0, 255]);
            DrawingMethods.roundedRect(_mc, false, 0, _height - 20, 20, 20, "0", [unselectedColor2, unselectedColor1], [50, 50], [0, 255]);
            
            _btn_back = BButton(addChild(new BButton(null, Create.ICON_ARROW_UP, null, 20, 20, false, false, false, pos, _scrollbarStyle)));
            _slider = Slider(addChild(new Slider(null, _width, _height - 44, _orientation, 0, pos, _style)));    
            _btn_fwd = BButton(addChild(new BButton(null, Create.ICON_ARROW_DOWN, null, 20, 20, false, false, false, pos, _scrollbarStyle)));
            
        }
        
        _btn_back.addEventListener(EVENT_CLICK, this, pageBack);
        _slider.addEventListener(EVENT_CHANGE, this, onChange);
        _btn_fwd.addEventListener(EVENT_CLICK, this, pageForward);
        _parentCell.addEventListener(EVENT_BLUR, onBlur);
        
        _btn_back.tabEnabled = _slider.tabEnabled = _btn_fwd.tabEnabled = false;

        Mouse.addListener(this);
        
    }
    
    //
    //
    private function updateComponents ():Void {
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) {
            
            _slider.ratio = _targetCell.width / _targetCell.contentWidth;
            
        } else {
            
            _slider.ratio = _targetCell.height / _targetCell.contentHeight;
            
        }

        if (_slider.ratio == 1) {
            _targetCell.maskContent = false;
            disable();
        } else {
            _targetCell.maskContent = true;
            enable();
        }
            
    }
    
    //
    //
    public function onChange (e:Event):Void {
        
        var contentMin:Number;
        var contentMax:Number = 0;
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) contentMin = targetCell.width - targetCell.contentWidth;
        else contentMin = targetCell.height - targetCell.contentHeight;
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) targetCell.contentX = Math.round(contentMin - contentMin * (1 - _slider.value));
        else targetCell.contentY = Math.round(contentMin - contentMin * (1 - _slider.value));

    }
    
    //
    //
    public function onTargetCellChange (e:Event):Void {
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) _pageRatio = Math.min(1, Math.max(0.01, targetCell.width / (targetCell.contentWidth - targetCell.width)));
        else _pageRatio = Math.min(1, Math.max(0.01, targetCell.height / (targetCell.contentHeight - targetCell.height)));
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) {
            
            _position.top = _targetCell.y + _targetCell.height;
            _position.left = _targetCell.x;
            
        } else {
            
            _position.top = _targetCell.y;
            _position.left = _targetCell.x + _targetCell.width;
            
        }
        
        x = _position.left;
        y = _position.top;
        
        updateComponents();
        
        var contentMin:Number;
 
        if (_orientation == Position.ORIENTATION_HORIZONTAL) contentMin = targetCell.width - targetCell.contentWidth;
        else contentMin = targetCell.height - targetCell.contentHeight;
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) _slider.value = 0 - (((0 - targetCell.contentX + contentMin) / contentMin) - 1);
        else _slider.value = 0 - (((0 - targetCell.contentY + contentMin) / contentMin) - 1);

    }
    
    //
    //
    public function onTargetCellShow (e:Event):Void {

        updateComponents();
        show();
        
    }
    
    //
    //
    public function onTargetCellHide (e:Event):Void {
        
        if (_targetCell.visible == false) hide();
        
    }
     
    //
    //
    public function enable ():Void {
        
        super.enable();
        
        _btn_back.show();
        _btn_fwd.show();
        _slider.show();
        
    }
    
    //
    //
    public function disable ():Void {

        super.disable();
        
        _btn_back.hide();
        _btn_fwd.hide();
        _slider.hide();
        
    }
    
    //
    //
    function pageForward (e:Event):Void {
        
        _slider.value += _pageRatio;
        onChange();

    }

    //
    //
    function center ():Void {
        
        _slider.value = 0.5;
        onChange();
        
    }
    
    //
    //
    function pageBack ():Void {
    
        _slider.value -= _pageRatio;
        onChange();

    }

    //
    //
    function onMouseWheel (delta:Number):Void {
        
        _slider.value -= delta / 60;
        onChange();

    }
    
    //
    //
    function onBlur (e:Event):Void {
        
        _slider.value = 0;
        onChange();
        
    }
    
}